package com.bjpowernode.controller;

import com.bjpowernode.service.FollowService;
import com.bjpowernode.user.Follow;
import com.bjpowernode.user.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping(value = "follow")
public class FollowController {

    @Resource
    private FollowService followService;

    @RequestMapping(value = "/add.do",produces = "application/text;charset=utf-8")
    @ResponseBody
    public String addFollow(@RequestParam("username") String username,@RequestParam("gzid") Integer gzid){
        Follow follow = new Follow();
        follow.setUsername(username);
        follow.setGzid(gzid);
        int result = followService.addFollow(follow);
        if (result == 0){
            return followService.removeFollow(follow) == 1 ? "已取消关注" : "取消关注失败";
        }
        return "已关注";
    }

    @RequestMapping(value = "/selectSelf.do")
    public ModelAndView selectSelf(@RequestParam("name") String name){
        ModelAndView mv = new ModelAndView();
        List<User> userList = followService.findSelfFollow(name);
        mv.addObject("userList",userList);
        mv.setViewName("userList");
        return mv;
    }

    @RequestMapping(value = "/selectSum.do")
    @ResponseBody
    public Integer selectSum(@RequestParam("username") String username){
        return followService.findFollowSum(username);
    }

    @RequestMapping(value = "/selectFansSum.do")
    @ResponseBody
    public Integer selectFansSum(@RequestParam("username") String username){
        return followService.findFansSum(username);
    }
}
